<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 依赖注入完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        .code-block pre {
            padding: 60px 30px 30px;
            margin: 0;
            overflow-x: auto;
        }
        .code-block code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            line-height: 1.6;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 700;
            margin-right: 1rem;
            flex-shrink: 0;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fadeInUp">
                Spring 依赖注入
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 animate-fadeInUp" style="animation-delay: 0.2s">
                掌握现代 Java 开发的核心技术
            </p>
            <div class="flex justify-center space-x-6 animate-fadeInUp" style="animation-delay: 0.4s">
                <div class="text-center">
                    <i class="fas fa-code text-3xl mb-2"></i>
                    <p class="text-sm">简洁优雅</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-puzzle-piece text-3xl mb-2"></i>
                    <p class="text-sm">松耦合</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-rocket text-3xl mb-2"></i>
                    <p class="text-sm">高效开发</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">S</span>pring框架提供了多种注入方式来实现依赖注入（Dependency Injection）。依赖注入是Spring框架的核心特性之一，它通过控制反转（IoC）的方式，让框架负责管理对象之间的依赖关系，从而实现松耦合的设计。以下是Spring中最常用的几种注入方式，每种方式都有其独特的应用场景和优势。
            </p>
        </div>

        <!-- Dependency Injection Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 text-center">
                <i class="fas fa-sitemap text-purple-600 mr-3"></i>
                依赖注入方式概览
            </h2>
            <div class="mermaid">
                graph TD
                    A[Spring 依赖注入] --> B[构造函数注入]
                    A --> C[Setter方法注入]
                    A --> D[字段注入]
                    A --> E[接口注入]
                    
                    B --> B1[推荐使用]
                    B --> B2[强制依赖]
                    B --> B3[不可变性]
                    
                    C --> C1[可选依赖]
                    C --> C2[灵活配置]
                    
                    D --> D1[简洁代码]
                    D --> D2[测试困难]
                    
                    E --> E1[较少使用]
                    E --> E2[特定场景]
                    
                    style A fill:#667eea,stroke:#764ba2,stroke-width:3px,color:#fff
                    style B fill:#48bb78,stroke:#38a169,stroke-width:2px,color:#fff
                    style C fill:#4299e1,stroke:#3182ce,stroke-width:2px,color:#fff
                    style D fill:#ed8936,stroke:#dd6b20,stroke-width:2px,color:#fff
                    style E fill:#9f7aea,stroke:#805ad5,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Injection Methods Grid -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <!-- Constructor Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">1</span>
                    <h3 class="text-2xl font-bold text-gray-800">构造函数注入</h3>
                </div>
                <p class="text-gray-600 mb-6">
                    通过构造函数参数来注入依赖项。这是Spring官方推荐的注入方式，因为它能确保依赖的强制性和不可变性。
                </p>
                <div class="code-block">
                    <pre><code>@Autowired
public ExampleService(ExampleRepository repository) {
    this.repository = repository;
}</code></pre>
                </div>
                <div class="mt-6 flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-green-100 text-green-800 rounded-full text-sm">
                        <i class="fas fa-check mr-1"></i>推荐使用
                    </span>
                    <span class="px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">
                        <i class="fas fa-lock mr-1"></i>不可变性
                    </span>
                    <span class="px-3 py-1 bg-purple-100 text-purple-800 rounded-full text-sm">
                        <i class="fas fa-shield-alt mr-1"></i>线程安全
                    </span>
                </div>
            </div>

            <!-- Setter Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">2</span>
                    <h3 class="text-2xl font-bold text-gray-800">Setter方法注入</h3>
                </div>
                <p class="text-gray-600 mb-6">
                    通过setter方法来注入依赖项。适用于可选依赖或需要在运行时动态改变依赖的场景。
                </p>
                <div class="code-block">
                    <pre><code>@Autowired
public void setExampleRepository(ExampleRepository repository) {
    this.repository = repository;
}</code></pre>
                </div>
                <div class="mt-6 flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-yellow-100 text-yellow-800 rounded-full text-sm">
                        <i class="fas fa-exchange-alt mr-1"></i>可选依赖
                    </span>
                    <span class="px-3 py-1 bg-indigo-100 text-indigo-800 rounded-full text-sm">
                        <i class="fas fa-cog mr-1"></i>灵活配置
                    </span>
                </div>
            </div>

            <!-- Field Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">3</span>
                    <h3 class="text-2xl font-bold text-gray-800">字段注入</h3>
                </div>
                <p class="text-gray-600 mb-6">
                    直接将依赖项注入到类的字段上。代码最简洁，但不利于单元测试，且违反了封装原则。
                </p>
                <div class="code-block">
                    <pre><code>@Autowired
private ExampleRepository repository;</code></pre>
                </div>
                <div class="mt-6 flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-orange-100 text-orange-800 rounded-full text-sm">
                        <i class="fas fa-code mr-1"></i>代码简洁
                    </span>
                    <span class="px-3 py-1 bg-red-100 text-red-800 rounded-full text-sm">
                        <i class="fas fa-exclamation-triangle mr-1"></i>测试困难
                    </span>
                </div>
            </div>

            <!-- Interface Injection -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-start mb-6">
                    <span class="section-number">4</span>
                    <h3 class="text-2xl font-bold text-gray-800">接口注入</h3>
                </div>
                <p class="text-gray-600 mb-6">
                    通过接口来注入依赖项。这种方式在Spring中较少使用，主要用于特定的框架集成场景。
                </p>
                <div class="code-block">
                    <pre><code>@Autowired
public void setExampleRepository(ExampleRepository repository) {
    this.repository = repository;
}</code></pre>
                </div>
                <div class="mt-6 flex flex-wrap gap-2">
                    <span class="px-3 py-1 bg-gray-100 text-gray-800 rounded-full text-sm">
                        <i class="fas fa-plug mr-1"></i>特定场景
                    </span>
                    <span class="px-3 py-1 bg-pink-100 text-pink-800 rounded-full text-sm">
                        <i class="fas fa-puzzle-piece mr-1"></i>框架集成
                    </span>
                </div>
            </div>
        </div>

        <!-- Best Practices Section -->
        <div class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-lightbulb text-yellow-500 mr-3"></i>
                最佳实践建议
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="text-center">
                    <div class="feature-icon mx-auto">
                        <i class="fas fa-hammer"></i>
                    </div>
                    <h4 class="font-bold text-lg mb-2">优先使用构造函数注入</h4>
                    <p class="text-gray-600 text-sm">确保依赖的完整性和不可变性，提高代码的可测试性</p>
                </div>
                <div class="text-center">
                    <div class="feature-icon mx-auto">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h4 class="font-bol